/*
 * Copyright (C) 2004, Velocityme Partnership
 * RoleActions.java
 *
 * Created on 09 October 2004, 05:39
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.*;
import com.velocityme.www.actionforms.MappedObjectsActionForm;
import com.velocityme.www.actionforms.nodeactionforms.RoleValueActionForm;
import java.util.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;

/**
 *
 * @author  Robert
 */
public class RoleActions extends NodeActions {
    
    /** Creates a new instance of RoleActions */
    public RoleActions() {
    }
    
    public ActionForward show(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        RolePK rolePK = new RolePK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("roleValue", cs.getRoleValue(key, rolePK));
        MappedObjectsActionForm permissionMap = new MappedObjectsActionForm();
        Set assignedPermissionValues = cs.getRoleAssignedPermissionValues(key, rolePK);
        Iterator i = cs.getAllPermissionValues(key).iterator();
        while (i.hasNext()) {
            Object permission = i.next();
            permissionMap.setObject(permission.toString(), new Boolean(assignedPermissionValues.contains(permission)));
        }
        session.setAttribute("permissionMap", permissionMap);
        return mapping.findForward("ACTION_DISPLAY");
    }
    
    public ActionForward showCreate(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("parentNodePK", nodePK);
        RoleValueActionForm roleValueActionForm = new RoleValueActionForm();
        Iterator i = cs.getAllPermissionValues(key).iterator();
        while (i.hasNext()) {
            PermissionValue permissionValue = (PermissionValue)i.next();
            roleValueActionForm.setPermissionId(permissionValue.toString(), permissionValue.getPermissionId());
            roleValueActionForm.setPermissionEnabled(permissionValue.toString(), Boolean.FALSE);
        }
        session.setAttribute("roleActionForm", roleValueActionForm);
        RoleValue roleValue = new RoleValue();
        roleValue.setNodeValue(new NodeValue());
        session.setAttribute("roleValue", roleValue);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward showEdit(ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        RolePK rolePK = new RolePK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        RoleValue roleValue = cs.getRoleValue(key, rolePK);
        session.setAttribute("roleValue", roleValue);
        RoleValueActionForm roleValueActionForm = new RoleValueActionForm();
        roleValueActionForm.setName(roleValue.getNodeValue().getName());
        roleValueActionForm.setDescription(roleValue.getNodeValue().getDescription());
        roleValueActionForm.setIsRecursive(roleValue.getIsRecursive());
        Set assignedPermissionValues = cs.getRoleAssignedPermissionValues(key, rolePK);
        Iterator i = cs.getAllPermissionValues(key).iterator();
        while (i.hasNext()) {
            PermissionValue permissionValue = (PermissionValue)i.next();
            roleValueActionForm.setPermissionId(permissionValue.toString(), permissionValue.getPermissionId());
            roleValueActionForm.setPermissionEnabled(permissionValue.toString(), new Boolean(assignedPermissionValues.contains(permissionValue)));
        }
        session.setAttribute("roleActionForm", roleValueActionForm);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward edit(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodePK nodePK;
        if (request.getParameter("btnSubmit").equals("OK")) {
            RoleValue roleValue = (RoleValue)session.getAttribute("roleValue");
            RoleValueActionForm roleValueActionForm = (RoleValueActionForm)form;
            roleValue.getNodeValue().setName(roleValueActionForm.getName());
            roleValue.getNodeValue().setDescription(roleValueActionForm.getDescription());
            roleValue.setIsRecursive(roleValueActionForm.getIsRecursive());
            // create the set of PermissionPKs assigned to the role
            Set permissionPKs = new HashSet();
            Iterator i = roleValueActionForm.getPermissions().iterator();
            while(i.hasNext()) {
                String permissionName = (String)i.next();
                Boolean enabled = roleValueActionForm.getPermissionEnabled(permissionName);
                if (enabled != null && enabled.equals(Boolean.TRUE))
                    permissionPKs.add(new PermissionPK(roleValueActionForm.getPermissionId(permissionName)));
            }
            RolePK rolePK;
            if (roleValue.roleIdHasBeenSet()) {
                rolePK = roleValue.getPrimaryKey();
                cs.roleEdit(key, rolePK, roleValue, permissionPKs);
            }
            else {
                NodePK parentNodePK = (NodePK)session.getAttribute("parentNodePK");
                rolePK = cs.roleCreate(key, parentNodePK, roleValue, permissionPKs);
            }
            nodePK = new NodePK(rolePK.getRoleId());
        }
        // Cancel simply displays the original node again
        else {
            nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        }
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
}
